//
//  ZyaPayListVC.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2021/3/18.
//  Copyright © 2021 zwb. All rights reserved.
//

import UIKit
 
class ZyaPayListVC: BaseTableViewController,UISearchBarDelegate {
    override func getSearchType() -> TableSearchType {
        return .hiddenx
    }
    var feedOrderList = [ZyaFeedOrderList]()
    private var searchBar:UISearchBar!
    var keyword = ""
    var type = "" // 1  饲料订单   2 付款申请
    init(type:String) {
        super.init(nibName: nil, bundle: nil)
        self.type = type
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        searchBar = UISearchBar(frame:CGRect(x:0,y:0,width:KSGlobal.screenW,height:40))
        searchBar.inputAccessoryView = KSToolbar(controller: self)
        searchBar.placeholder = "搜索"
        searchBar.delegate = self
        tableView.tableHeaderView = searchBar
        
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 90
        tableView.separatorStyle = .none
        tableView.backgroundColor = Theme.tableViewColor
        tableView.register(ZyaPayListCell.self, forCellReuseIdentifier: ZyaPayListCell.identifier)
 
        tableView.mj_header?.beginRefreshing()
    }
    
    override func network() {
        
        var api:API?
        if type == "1" {
            api = API.getOrdersAudit(page: "\(self.page)", size: "\(KSGlobal.pageSize)", s_orderform_no: keyword)
        } else {
            api = API.payApplyQuery(page: "\(self.page)", size: "\(KSGlobal.pageSize)", s_orderform_no: keyword)
        }
     
        KSNetwork.shared.doRequest(api!) { (data:BaseResult<ZyaFeedOrderList>) in
            if data.flag == TRUE {
                if let infos = data.infos {
                    self.tableView.mj_header!.endRefreshing()
                    self.tableView.mj_footer!.endRefreshing()
                    
                    if self.page == 1 {
                        self.feedOrderList.removeAll()
                        self.tableView.reloadData()
                    }
                 
                    if infos.count < KSGlobal.pageSize {
                        self.tableView.mj_footer?.endRefreshingWithNoMoreData()
                    }
                    
                    infos.forEach {
                        self.feedOrderList.append($0)
                    }
                    
                    self.page += 1
                    self.tableView.reloadData()
                }
            }
            
        }
        
      
    }
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
  
        self.tableView.resignFirstResponder()
         
    }
    //searchBar编辑
    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        self.keyword = searchBar.text.unwrap
        self.tableView.mj_header?.beginRefreshing()
        
    }
    
 
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return feedOrderList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: ZyaPayListCell.identifier) as? ZyaPayListCell else { fatalError() }
        if self.type == "1" {
            cell.siliaoModel = feedOrderList[indexPath.row]
        } else {
            cell.payModel = feedOrderList[indexPath.row]
        }
        
        cell.deleteEvent = { [unowned self] in
            guard let id = self.feedOrderList[indexPath.row].idKey else {
                return
            }
            
            let alert = UIAlertController(title: "确认删除", message: "", preferredStyle: .alert)
            let delete = UIAlertAction(title: "确定", style: .default) { (_) in
                
                KSNetwork.shared.doRequest(API.payApplyDelete(id_key: id), successHandle: { (data: BaseResult<OperateResult>) in
                    KSToast(data.message, duration: 1.0, position: .center)
                    if data.message == "删除成功" {
                        self.tableView.beginUpdates()
                        self.feedOrderList.remove(at: indexPath.row)
                        self.tableView.deleteRows(at: [indexPath], with: .automatic)
                        self.tableView.endUpdates()
                        self.tableView.reloadData()
                    }
                })
                
            }
            
            let cancel = UIAlertAction(title: "取消", style: .cancel, handler: nil)
            alert.addAction(delete)
            alert.addAction(cancel)
            self.present(alert, animated: true, completion: nil)
        }
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if self.type == "1" {
            let vc = ZyaSiliaoContentVC()
            vc.model =  self.feedOrderList[indexPath.row]
            self.navigationController?.pushViewController(vc, animated: true)
        } else {
            let vc = ZyaPayContentVC()
            vc.model =  self.feedOrderList[indexPath.row]
            self.navigationController?.pushViewController(vc, animated: true)
            
        }
       
       
    }
}


